<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>实例列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="/x-admin/css/font.css">
    <link rel="stylesheet" href="/x-admin/css/xadmin.css">

    <link rel="stylesheet" href="/x-admin/lib/layui/css/modules/layer/default/layer.css">

    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>

    <script type="text/javascript" src="/x-admin/lib/layui/layui.js" charset="utf-8"></script>

    <script type="text/javascript" src="/x-admin/lib/layui/lay/modules/layer.js" charset="utf-8"></script>

    <script type="text/javascript" src="/x-admin/js/xadmin.js"></script>
    <script type="text/javascript" src="/x-admin/js/vue.js"></script>
    <script type="text/javascript" src="/x-admin/js/axios.js"></script>
    <script type="text/javascript" src="/x-admin/js/common.js"></script>
    <link rel="stylesheet" href="https://unpkg.com/element-ui@2.2.2/lib/theme-chalk/index.css">
    <script src="https://unpkg.com/element-ui@2.2.2/lib/index.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="x-nav">
        <span class="layui-breadcrumb">
            <a href="/page/welcome">起始页</a>
            <a href="/container/page/list"><cite>实例列表</cite></a>
        </span>

    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       href="javascript:window.location.reload();" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i>
    </a>
</div>
<div class="x-body">
    <div class="layui-row">
        <el-form :inline="true">


            <el-form-item label="物理机">
                <el-select v-model="searchModel.machineId" placeholder="请选择">
                    <el-option
                            v-for="machine in data"
                            :key="machine.name"
                            :label="machine.machineName"
                            :value="machine.id">
                    </el-option>
                </el-select>
            </el-form-item>


            <el-button icon="el-icon-search" circle @click="getDatas(1)"></el-button>
        </el-form>
    </div>
    <xblock>
        <button class="layui-btn" onclick="window.location.href='/container/page/add'"><i class="layui-icon"></i>添加
        </button>
        <span class="x-right" style="line-height:40px">共有数据：{{dataModel.total}} 条</span>
    </xblock>
    <table class="layui-table">
        <thead>
        <tr>
            <th>序号</th>
            <th>ID</th>
            <th>镜像Id</th>
            <th>镜像名字</th>
            <th>启动参数</th>
            <th>容器Id</th>
            <th>运行状态</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <tr v-for="(data, index) in dataModel.list">
            <td>{{index+1}}</td>
            <td>{{data.id}}</td>
            <td>{{data.imgId}}</td>
            <td>{{data.imgName}}</td>
            <td>{{data.cmdParameter}}</td>
            <td>{{data.containerId}}</td>
            <td>{{data.runState?"运行中":"关机中"}}</td>

            <td>
                <el-button @click="startContainer(data.id)">启动容器</el-button>
                <el-button @click="deleteData(data.id,index)">关闭删除</el-button>
            </td>
        </tr>
        </tbody>
    </table>
    <div class="page">
        <div>
            <a class="prev" href="#" v-if="dataModel.hasPreviousPage"
               @click="getDatas(dataModel.pageNum-1)">&lt;&lt;</a>
            <a class="num" href="#" v-if="dataModel.hasPreviousPage"
               @click="getDatas(dataModel.pageNum-1)">{{dataModel.pageNum-1}}</a>
            <span class="current">{{dataModel.pageNum}}</span>
            <a class="num" href="#" v-if="dataModel.hasNextPage" @click="getDatas(dataModel.pageNum+1)">{{dataModel.pageNum+1}}</a>
            <a class="num" href="#" v-if="dataModel.pageNum+2<dataModel.pages"
               @click="getDatas(dataModel.pages)">{{dataModel.pages}}</a>
            <a class="next" href="#" v-if="dataModel.hasNextPage" @click="getDatas(dataModel.pageNum+1)">&gt;&gt;</a>
        </div>
    </div>

</div>
<script type="text/javascript">
    //axios http库
    var instance = axios.create({
        baseURL: "",
        timeout: 5000
    });
    axios.interceptors.request.use(function (config) {
        return config;
    }, function () {
        console.error("request阶段处理异常", error)
        return Promise.reject(error);
    })


    //axios response interceptor
    instance.interceptors.response.use(function (res) {
        console.log(res)
        if (res && res.data && res.data.code === 200 || res.data.code === 0)
            return res;
        else {
            layer.msg("请求异常" + res.data.msg);
            return Promise.reject(res.data);
        }
        return res;
    }, function (error) {
        layer.msg("网络处理异常")
        console.error("网络处理异常", error)
        return Promise.reject(error);
    })

    //set vue use axios
    Vue.prototype.$http = instance;


    $(function () {

        var vue = new Vue({
            el: '.x-body',
            data: {
                dataModel: [],
                searchModel: {},
                data: []
            },
            methods: {
                getDatas: function (page) {
                    var thiz = this;
                    this.$http.post("/container/search" + "?" + getPageQueryStr(page, 10), clearEmptyValue(this.searchModel)).then(function (data) {
                        var data = data.data.data;
                        thiz.dataModel = data;
                    })
                },
                deleteData: function (id, index) {
                    if (!id)
                        alert("删除失败，Id数据缺失！")
                    var thiz = this;
                    var confirmResult = confirm("确认要删除数据！")
                    if (confirmResult) {
                        this.$http.delete("/container/" + id).then(function (data) {
                            alert("删除成功！");
                            thiz.dataModel.list.splice(index, 1);
                        })
                    }
                },
                toUpdatePage: function (id) {
                    window.location.href = "/container/page/update?id=" + id;
                },
                toAddPage: function () {
                    window.location.href = "/container/page/update?id=add"
                },
                handleUploadSuccess: function () {
                    this.dataModel.img = (file && file[0]) ? (fileResourcesDomain + file[0].filepath) : "";
                },
                startContainer: function (id) {
                    this.$http.get("/container/start?containerId=" + id).then(function () {
                        if (data.data.data) {
                            alert("容器启动成功！")
                        } else {
                            alert("容器启动失败！！" + date.data.msg)
                        }
                    })
                },
                getMachine: function () {
                    var thiz = this;
                    this.$http.get("/machine?size=1000").then(function (data) {
                        thiz.data = data.data.data.list;
                    })
                },
            },
            updated: function () {
                $('.tablelist tbody tr:odd').addClass('odd');
            },
            mounted: function () {
                this.getDatas();
                this.getMachine();
            }
        });
    });
</script>

</body>

</html>